import i18n from '@/i18n'
import { MetaPlatformDao } from '@apps/meta/domain/meta-platform/meta-platform.dao'
import { MetaResourceChartDao } from '@apps/meta/domain/meta-resource-chart/meta-resource-chart.dao'
import { MetaResourceModuleDao } from '@apps/meta/domain/meta-resource-module/meta-resource-module.dao'
import { DomainModel } from '@apps/meta/domain/model'
import { Assist } from '@libs/lhy-common'
import { IgnoreParameterSerialize } from '@libs/lhy-core'
import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { MetaPlatformApiService } from '../../meta-platform-api/meta-platform-api.service'
import { MetaPlatformAuthDto } from './dto/meta-platform-auth.dto'
import { MetaPlatformGetBindDataSourceTypeDto } from './dto/meta-platform-get-bind-data-source-type.dto'
import { MetaPlatformGetBindDataSourceDto } from './dto/meta-platform-get-bind-data-source.dto'
import { MetaPlatformGetChartDataDto } from './dto/meta-platform-get-chart-data.dto'
import { MetaPlatformGetHistoryChartDataDto } from './dto/meta-platform-get-history-chart-data.dto'
import { MetaPlatformGetRealtimeDataDto } from './dto/meta-platform-get-realtime-data.dto'
import { MetaPlatformListDto } from './dto/meta-platform-list.dto'
import { MetaPlatformRequestResourceModuleActionDto } from './dto/meta-platform-request-resource-module-action.dto'
import { MetaPlatformSaveDto } from './dto/meta-platform-save.dto'
import { MetaPlatformSendCommandDto } from './dto/meta-platform-send-comman.dto'
import { MetaPlatformUpdateByIdDto } from './dto/meta-platform-update-by-id.dto'
import { MetaPlatformVerifyControlPasswordDto } from './dto/meta-platform-verify-control-password.dto'
import { MetaPlatformWsAuthDto } from './dto/meta-platform-ws-auth.dto'
import { MetaPlatformGetBindDataSourceTypeVo } from './vo/meta-platform-get-bind-data-source-type.vo'
import { MetaPlatformGetBindDataSourceVo } from './vo/meta-platform-get-bind-data-source.vo'
import { MetaPlatformGetChartDataVo } from './vo/meta-platform-get-chart-data.vo'
import { MetaPlatformGetHistoryChartDataVo } from './vo/meta-platform-get-history-chart-data.vo'
import { MetaPlatformGetRealtimeDataVo } from './vo/meta-platform-get-realtime-data.vo'
import { MetaPlatformRequestResourceModuleActionVo } from './vo/meta-platform-request-resource-module-action.vo'

@ApiTags(i18n.t('app-meta.meta-platform.meta-platform.12152177-0'))
@Controller({
  path: 'platform',
  version: '1',
})
export class MetaPlatformController {
  constructor(
    private readonly platformApiService: MetaPlatformApiService,
    private readonly platformDao: MetaPlatformDao,
    private readonly resourceChartDao: MetaResourceChartDao,
    private readonly resourceModuleDao: MetaResourceModuleDao
  ) {}

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-1') })
  @Get('list')
  async list(@Query() param: MetaPlatformListDto): Promise<DomainModel.MetaPlatform[]> {
    return this.platformDao.list({
      ...param,
      label: {
        contains: param.label,
      },
    })
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-2') })
  @Put()
  async save(@Body() param: MetaPlatformSaveDto): Promise<void> {
    Assist.threw(
      await this.platformDao.isPlatformExists({ platform: param.platform }),
      i18n.t('app-meta.meta-platform.meta-platform.12152177-3', { args: [param.platform] })
    )
    Assist.threw(
      await this.platformDao.isLabelExists({ label: param.label }),
      i18n.t('app-meta.meta-platform.meta-platform.12152177-4', { args: [param.label] })
    )

    await this.platformDao.save(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-5') })
  @Post()
  async updateById(@Body() param: MetaPlatformUpdateByIdDto): Promise<void> {
    Assist.threw(
      await this.platformDao.isLabelExists({ label: param.label, id: param.id }),
      i18n.t('app-meta.meta-platform.meta-platform.12152177-4', { args: [param.label] })
    )
    const oldData = Assist.notNull(
      await this.platformDao.getById(param.id),
      i18n.t('app-meta.meta-platform.meta-platform.12152177-6', { args: [param.id] })
    )

    await this.platformDao.updateById(param.id, param)
    this.platformApiService.refresgPlatform(oldData.platform)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-7') })
  @Delete()
  async deleteById(@Query('id') id: number): Promise<void> {
    const oldData = Assist.notNull(
      await this.platformDao.getById(id),
      i18n.t('app-meta.meta-platform.meta-platform.12152177-8', { args: [id] })
    )

    const resourceCharts = await this.resourceChartDao.list({ platform: oldData.platform })
    Assist.threw(resourceCharts.length > 0, i18n.t('app-meta.meta-platform.meta-platform.12152177-9'))

    const resourceModules = await this.resourceModuleDao.list({ platform: oldData.platform })
    Assist.threw(resourceModules.length > 0, i18n.t('app-meta.meta-platform.meta-platform.12152177-10'))

    await this.platformDao.deleteById(id)
    this.platformApiService.refresgPlatform(oldData.platform)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-11') })
  @Post('auth')
  auth(@Body() param: MetaPlatformAuthDto): Promise<void> {
    return this.platformApiService.requestAuth(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-12') })
  @Post('ws_auth')
  wsAuth(@Body() param: MetaPlatformWsAuthDto): Promise<void> {
    return this.platformApiService.requestWsAuth(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-13') })
  @Post('get_realtime_data')
  @IgnoreParameterSerialize({ response: true })
  getRealtimeData(@Body() param: MetaPlatformGetRealtimeDataDto): Promise<MetaPlatformGetRealtimeDataVo> {
    return this.platformApiService.requestGetRealtimeData(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-14') })
  @Post('send_command')
  sendCommand(@Body() param: MetaPlatformSendCommandDto): Promise<void> {
    return this.platformApiService.requestSendCommand(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-15') })
  @Post('verify_control_password')
  verifyControlPassword(@Body() param: MetaPlatformVerifyControlPasswordDto): Promise<void> {
    return this.platformApiService.requestVerifyControlPassword(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-16') })
  @Post('get_chart_data')
  async getChartData(@Body() param: MetaPlatformGetChartDataDto): Promise<MetaPlatformGetChartDataVo> {
    const data = await this.platformApiService.requestResourceChart(param)
    if (data.dataHash && data.dataHash === param.dataHash) {
      return null
    }
    return data
  }

  @ApiOperation({ summary: i18n.t('meta-platform.meta-platform.559145-0') })
  @Post('get_history_chart_data')
  async getHistoryChartData(
    @Body() param: MetaPlatformGetHistoryChartDataDto
  ): Promise<MetaPlatformGetHistoryChartDataVo> {
    const data = await this.platformApiService.requestResourceHistoryChart(param)
    if (data.dataHash && data.dataHash === param.dataHash) {
      return null
    }
    return data
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-17') })
  @Post('request_resource_module_action')
  @IgnoreParameterSerialize({ response: true })
  requestResourceModuleAction(
    @Body() param: MetaPlatformRequestResourceModuleActionDto
  ): Promise<MetaPlatformRequestResourceModuleActionVo> {
    return this.platformApiService.requestResourceModuleAction(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-18') })
  @Post('get_bind_data_source')
  getBindDataSource(@Body() param: MetaPlatformGetBindDataSourceDto): Promise<MetaPlatformGetBindDataSourceVo[]> {
    return this.platformApiService.requestGetBindDataSource(param)
  }

  @ApiOperation({ summary: i18n.t('app-meta.meta-platform.meta-platform.12152177-19') })
  @Post('get_bind_data_source_type')
  getBindDataSourceType(
    @Body() param: MetaPlatformGetBindDataSourceTypeDto
  ): Promise<MetaPlatformGetBindDataSourceTypeVo> {
    return this.platformApiService.requestGetBindDataSourceType(param)
  }
}
